package com.exmple.testespresso

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.exmple.testespresso.activity.MainActivity
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/*
    kotlin 和新的 ActivityTestRule ：@Rule 必须是公开的
    https://www.codingdict.com/questions/232165

 */
@RunWith(AndroidJUnit4::class)
class MainActivityEspressoTest {
    companion object{
        const val STRING_TO_BE_TYPED = "6660001000"
    }

    @get:Rule
    var mActivityTestRule = ActivityTestRule(MainActivity::class.java)

    @Test
    fun listGoesOverTheFold() {
        onView(ViewMatchers.withText("Hello World!"))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
    }

    @Test
    fun clickTest() {
        onView(ViewMatchers.withId(R.id.tv_click))
            .perform(ViewActions.click())
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()))// 判断 tv_click 空间是否显示

        onView(ViewMatchers.withId(R.id.tv_open_second)).perform(ViewActions.click())//打开第二个页面
        onView(ViewMatchers.withId(R.id.tv_click_close)).perform(ViewActions.click())//关闭第二个页面
    }

    @Test
    fun matchText() {
        onView(ViewMatchers.withId(R.id.tv_click)).perform(ViewActions.click())//执行点击事件
        onView(ViewMatchers.withId(R.id.tv_text)).check(ViewAssertions.matches(ViewMatchers.withText("新的内容")))//判断 tv_text 的内容是否为 新的内容
    }

    @Test
    fun changeText() {
        onView(ViewMatchers.withId(R.id.login_et_moiblie_no)).perform(ViewActions.typeText(STRING_TO_BE_TYPED), ViewActions.closeSoftKeyboard())//输入框里自动输入的内容是 STRING_TO_BE_TYPED 的值
        onView(ViewMatchers.withId(R.id.tv_click)).perform(ViewActions.click())//执行点击事件
        onView(ViewMatchers.withId(R.id.tv_text)).check(ViewAssertions.matches(ViewMatchers.withText(STRING_TO_BE_TYPED)))//判断 tv_text 的内容是否为 STRING_TO_BE_TYPED 的值

    }
}